﻿namespace devMobile.Controls.Charts
{
    using System.Windows.Shapes;

    using Microsoft.Expression.Media;
    using Microsoft.Expression.Shapes;

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Media;

    public partial class PieChartControl : UserControl
    {
        private Dictionary<string, int> _itemsSource;
        public Dictionary<string, int> ItemsSource
        {
            get
            {
                return _itemsSource;
            }
            set
            {
                _itemsSource = value;
                Dispatcher.BeginInvoke(this.Draw);
            }
        }


        public PieChartControl()
        {
            InitializeComponent();
        }

        void Draw()
        {
            MainGrid.ShowGridLines = true;

            var total = this.ItemsSource.Sum(item => item.Value);
            var lastStartAngle = default(double);
            var myScrollViewer = new ScrollViewer
                {
                    VerticalScrollBarVisibility = ScrollBarVisibility.Auto,
                    HorizontalScrollBarVisibility = ScrollBarVisibility.Disabled
                };

            var stackPanel = new StackPanel
                {
                    VerticalAlignment = VerticalAlignment.Center,
                    HorizontalAlignment = HorizontalAlignment.Left,
                    Margin = new Thickness(2, 8, 2, 8),
                    MaxWidth = this.MainGrid.ActualWidth * 0.4
                };

            foreach (var item in this.ItemsSource)
            {
                var columnDefinition = new ColumnDefinition
                    { Width = new GridLength(MainGrid.ActualWidth * 0.6, GridUnitType.Pixel), MaxWidth = 480 };
                MainGrid.ColumnDefinitions.Add(columnDefinition);

                var color = new Color().Random();

                var arc = new Arc
                    {
                        Margin = new Thickness(10, 10, 10, 10), 
                        StartAngle = lastStartAngle, 
                        EndAngle = lastStartAngle + (item.Value * 360) / total, 
                        Fill = new SolidColorBrush(color), 
                        ArcThicknessUnit = UnitType.Percent, 
                        ArcThickness = 2.0d, 
                        Stroke = new SolidColorBrush(Colors.Black),
                        StrokeThickness = 1.0
                    };
                arc.SetValue(Grid.ColumnProperty, 1);
                
                MainGrid.Children.Add(arc);
                lastStartAngle = arc.EndAngle;
                
                var gridCol = new Grid();

                gridCol.Children.Add(
                    new Rectangle
                        {
                            Fill = new SolidColorBrush(color),
                            Margin = new Thickness(2, 2, 2, 2),
                            Stroke = new SolidColorBrush(Colors.Black),
                            StrokeThickness = 1.0
                        });
                var textBlock = new TextBlock
                    {
                        Text = string.Format("{0} - ({1}%)", item.Key, (item.Value * 100) / total),
                        TextWrapping = TextWrapping.Wrap,
                        Margin = new Thickness(3, 3, 3, 3)
                    };
                gridCol.Children.Add(textBlock);
                stackPanel.Children.Add(gridCol);
                arc.Tap += (sender, o) =>
                    {
                        //foreach (var control in MainGrid.Children)
                        //    if (control.GetType() == typeof(TextBlock))
                        //    {
                        //        (control as TextBlock).FontWeight = FontWeights.Normal;
                        //    }
                        //textBlock.FontWeight = FontWeights.Bold;
                    };
                myScrollViewer.Content = stackPanel;
            }
            myScrollViewer.SetValue(Grid.ColumnProperty, 2);
            MainGrid.Children.Add(myScrollViewer);
        }
    }
}
